package com.itrip.service.impl;

import com.itrip.common.MD5;
import com.itrip.common.UserAgentUtil;
import com.itrip.entity.ItripUser;
import com.itrip.service.TokenService;
import cz.mallat.uasparser.UserAgentInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class TokenServiceImpl implements TokenService {
    @Autowired
    private RedisTemplate<String,Object>redisTemplate;

    @Override
    public String generateToken(String agent, ItripUser user) {
        //agent 客户端请求时，有一个头信息，包含了请求客户端的一些信息
        try {
            UserAgentInfo userAgentInfo = UserAgentUtil.getUasParser().parse(agent);
            String deviceType = userAgentInfo.getDeviceType();
            StringBuffer tokenStr = new StringBuffer();
            tokenStr.append("token:");
            if(deviceType.toLowerCase().contains("mobile")){
                //手机端
                tokenStr.append("MOBLIE");
            }else{
                tokenStr.append("PC");
            }
            //拼接USERCODE
            String md5 = MD5.getMd5(user.getUserCode(),32);
            tokenStr.append("-").append(md5);
            //拼接USERID
            tokenStr.append("-").append(user.getId());
            //拼接时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            tokenStr.append("-").append(sdf.format(new Date()));
            //拼接对agent进行md5的值
            tokenStr.append(MD5.getMd5(agent,6));
            return tokenStr.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void save(String token, ItripUser user) {
        try{
            if(token.contains("MOBILE")){
                //手机端可以不过期。
                redisTemplate.opsForValue().set(token,user);
            }else{
                //电脑端2小时过期
                redisTemplate.opsForValue().set(token,user,2, TimeUnit.HOURS);
            }
        }catch(Exception e){
            log.error("get userinfo from redis but is error : " + e.getMessage());
        }
    }

    @Override
    public ItripUser load(String token) {
        return null;
    }

    @Override
    public void delete(String token) {

    }

    @Override
    public String replaceToken(String agent, String token) throws Exception {
        return null;
    }

    @Override
    public boolean validate(String agent, String token) {
        return false;
    }

    public static void main(String[] args) throws IOException {

        UserAgentInfo userAgentInfo = UserAgentUtil.getUasParser().parse(
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"
                 );
        String deviceType = userAgentInfo.getType();
        System.out.println(deviceType);
    }
}
